<?php
/**
 * Created by PhpStorm.
 * User: sunqiang
 * Date: 2018/8/17
 * Time: 下午7:06
 */

namespace wap\controllers;


use Yii;
use common\helps\api;
use yii\data\ArrayDataProvider;

class ReportController extends CommonController
{


    public function actionShopSale()
    {
        $user = $this->getLoginUser();


        if (Yii::$app->request->isGet) {

            $this->userName = $user['userName'];

            return $this->render('shop_sale');
        } else if (Yii::$app->request->isAjax) {
            $inputParam = Yii::$app->request->post();

            if (isset($inputParam['start_date']) && !empty($inputParam['start_date'])) {
                $start_date = $inputParam['start_date'];
            } else {
                $start_date = date('Y-m-d');
            }


            if (isset($inputParam['end_date']) && !empty($inputParam['end_date'])) {
                $end_date = date('Y-m-d', strtotime('+1 day', strtotime($inputParam['end_date'])));
            } else {
                $end_date = date('Y-m-d', strtotime('+1 day', strtotime($start_date)));
            }

            $count_days = $this->count_days($start_date, $end_date);

            if($count_days<=0) {
                $this->retJson('错误! 结束日期必须大于开始日期!', 500);
            }
            if($count_days>60) {
                $this->retJson('错误! 查询间隔最多为60天', 500);
            }


            $data = $this->getReportOrderShopSale($start_date, $end_date);

            if (null != $data) {

                foreach ($data as &$item) {

                    $amt_total = $item['amt_ele'] + $item['amt_mt'] + $item['amt_stay'] + $item['amt_delivery'] + $item['amt_syj'];
                    $qty_total = $item['qty_order_ele'] + $item['qty_order_mt'] + $item['qty_order_stay'] + $item['qty_order_delivery'] + $item['qty_order_syj'];
                    $item['amt_total'] = sprintf("%.2f", $amt_total);
                    $item['qty_total'] = $qty_total;

                }

                $this->retJson('ok', 200, $data);
            } else {
                $this->retJson('系统错误,请重试', 500);
            }
        }

    }


    public function actionShopProductSale()
    {
        $user = $this->getLoginUser();


        if (Yii::$app->request->isGet) {

            $this->userName = $user['userName'];

            return $this->render('shop_product_sale');
        } else if (Yii::$app->request->isAjax) {
            $inputParam = Yii::$app->request->post();

            if (isset($inputParam['start_date']) && !empty($inputParam['start_date'])) {
                $start_date = $inputParam['start_date'];
            } else {
                $start_date = date('Y-m-d');
            }

            if (isset($inputParam['end_date']) && !empty($inputParam['end_date'])) {
                $end_date = date('Y-m-d', strtotime('+1 day', strtotime($inputParam['end_date'])));
            } else {
                $end_date = date('Y-m-d', strtotime('+1 day', strtotime($start_date)));
            }


            $count_days = $this->count_days($start_date, $end_date);

            if($count_days<=0) {
                $this->retJson('错误! 结束日期必须大于开始日期!', 500);
            }
            if($count_days>60) {
                $this->retJson('错误! 查询间隔最多为60天', 500);
            }


            $data = $this->getReportOrderShopProductSale($start_date, $end_date);

            if (null != $data) {
                $this->retJson('ok', 200, $data);
            } else {
                $this->retJson('系统错误,请重试', 500);
            }
        }

    }


    private function getReportOrderShopSale($start_date, $end_date)
    {
        $user = $this->getLoginUser();
        if (isset($user) && !empty($user)) {

            $data = array();

            $shops = $user['shop'];
            foreach ($shops as $shop) {
                $param = array(
                    'start_date' => $start_date,
                    'end_date' => $end_date,
                    'shopId' => $shop['shopId']
                );

                $result = api::post('report-order/shop-sale', $param);
                if ($result['code'] == 200) {
                    $data = array_merge($data, $result['data']);
                }
            }
            return $data;
        }
        return null;
    }

    private function getReportOrderShopProductSale($start_date, $end_date)
    {
        $user = $this->getLoginUser();
        if (isset($user) && !empty($user)) {

            $data = array();

            $shops = $user['shop'];
            foreach ($shops as $shop) {
                $param = array(
                    'start_date' => $start_date,
                    'end_date' => $end_date,
                    'shopId' => $shop['shopId']
                );


                $shopItem = array(
                    'shopId' => $shop['shopId'],
                    'shopName' => $shop['shopName'],
                    'productSale' => array()
                );


                $result = api::post('report-order/shop-product-sale', $param);
                if ($result['code'] == 200) {
                    $shopItem['productSale'] = $result['data'];
                }
                $data[] = $shopItem;
            }
            return $data;
        }
        return null;
    }


    private function count_days($a, $b)
    {
        $d1 = strtotime($a);
        $d2 = strtotime($b);
        $Days = round(($d2 - $d1) / 3600 / 24);
        return $Days;
    }
}